package com.example.demo.config;

import com.example.demo.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: ErrorHandler
 * @author: 芃
 * @description:
 * @date: 2024/12/30 12:06
 */
@ControllerAdvice
@Slf4j
@ResponseBody
public class ErrorHandler {

    //抛出异常时优先匹配最小的异常类型


    @ExceptionHandler
    public Result exception(Exception e){
        log.error("发生异常："+e);
        return Result.fail("系统异常，请联系管理员");
    }

    @ExceptionHandler
    public Result exception(NullPointerException e){
        log.error("发生异常："+e);
        return Result.fail("空指针异常");
    }


    @ExceptionHandler
    public Result exception(ArithmeticException e){
        log.error("发生异常："+e);
        return Result.fail("算数异常");
    }

}
